package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@Api(tags = "文件上传")
@Slf4j
@RequiredArgsConstructor
public class CommonController {

    private final AliOssUtil aliOssUtil;



    @PostMapping("/admin/common/upload")
    public Result upload(MultipartFile file) throws IOException {

        //避免文件名重复
        String filename = file.getOriginalFilename();
        String postfix = filename.substring(filename.lastIndexOf("."));
        String newFileName = UUID.randomUUID()+postfix;

        String url = aliOssUtil.upload(file.getBytes(), newFileName);
        return Result.success(url);

    }
}
